#!/usr/bin/env python

#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

class hexHashIter:
	"""
	Will sequentially generate hex strings of a given length.
	"""
	def __init__(self, length):
		self.hex = []
		self.maxIndex = 0
		self.reset(length)

	def getString(self):
		# returns a hex string
		hexDict = { 0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',
			10:'a',11:'b',12:'c',13:'d',14:'e',15:'f'}
		hexStr = ''
		for h in range(0,self.maxIndex + 1):
			hexStr += hexDict[self.hex[h]]
		return hexStr

	def increment(self, index = -2):
		if index < -1: index = self.maxIndex
		# return 1 if successful, 0 if done iterating
		if index == -1:
			return 0
		if self.hex[index] > 14:
			self.hex[index] = 0
			return self.increment(index - 1)
		else:
			self.hex[index] += 1
			return 1

	def reset(self, length):
		self.hex = []
		self.maxIndex = length - 1
		for h in range(0,length): self.hex.append(0)
		

# eof

